/*
 * Copyright (C) 2018 Logos Technology Ltd.
 */

package com.logos.tool.image;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.logos.tool.ApplicationUtil;
import com.logos.tool.R;
import com.logos.tool.glide.GlideApp;
import com.logos.tool.glide.OnLoadImageBitmapListener;
import com.logos.tool.glide.OnLoadImageFileListener;

import java.io.File;

/**
 * @author Young Ken
 */

public class ImageLoadUtils {
    /**
     * 加载base64图片
     *
     * @param context        context
     * @param imageByteArray imageByteArray
     * @param imageView      imageView
     */
    public static void loadImageView(@NonNull Context context, @Nullable byte[] imageByteArray,
                                     @NonNull ImageView imageView) {
        GlideApp.with(context).asBitmap().load(imageByteArray).into(imageView);
    }

    /**
     * 默认加载
     *
     * @param context   context
     * @param path      path
     * @param imageView view
     */
    public static void loadImageView(@NonNull Context context, @Nullable String path,
                                     @NonNull ImageView imageView) {
        loadImageView(context, path, imageView, new ColorDrawable(context.getResources().getColor(R.color.color_custom)),
                ApplicationUtil.getContext().getResources()
                        .getDrawable(R.mipmap.ic_file_fail));
    }

    /**
     * 默认加载
     *
     * @param context   context
     * @param object    o
     * @param imageView view
     */
    public static void loadImageView(@NonNull Context context, @Nullable Object object,
                                     @NonNull ImageView imageView) {
        loadImageView(context, object, imageView, new ColorDrawable(context.getResources().getColor(R.color.color_custom)),
                ApplicationUtil.getContext().getResources()
                        .getDrawable(R.mipmap.ic_file_fail));
    }

    /**
     * 默认加载
     *
     * @param context   context
     * @param object    o
     * @param imageView view
     */
    public static void loadImageView(@NonNull Context context, @Nullable String object,
                                     @NonNull ImageView imageView, @Nullable RequestOptions options) {
        loadImageView(context, object, imageView, new ColorDrawable(context.getResources().getColor(R.color.color_custom)),
                ApplicationUtil.getContext().getResources()
                        .getDrawable(R.mipmap.ic_file_fail), options);
    }

    /**
     * 设置加载中以及加载失败图片
     *
     * @param context        context
     * @param path           path
     * @param imageView      view
     * @param loadingImage   image
     * @param errorImageView view
     */
    public static void loadImageView(@NonNull Context context, @Nullable String path,
                                     @NonNull ImageView imageView,
                                     @DrawableRes int loadingImage,
                                     @DrawableRes int errorImageView) {
        loadImageView(context, path, imageView, context.getResources()
                .getDrawable(loadingImage), context.getResources()
                .getDrawable(errorImageView));
    }

    /**
     * 设置加载中以及加载失败图片
     *
     * @param context        context
     * @param object         o
     * @param imageView      view
     * @param loadingImage   image
     * @param errorImageView view
     */
    public static void loadImageView(@NonNull Context context, @Nullable Object object,
                                     @NonNull ImageView imageView,
                                     @DrawableRes int loadingImage,
                                     @DrawableRes int errorImageView) {
        loadImageView(context, object, imageView, context.getResources()
                .getDrawable(loadingImage), context.getResources().getDrawable(errorImageView));
    }

    /**
     * 设置加载中以及加载失败图片
     *
     * @param context        context
     * @param path           path
     * @param imageView      view
     * @param loadingImage   image
     * @param errorImageView view
     */
    public static void loadImageView(@NonNull Context context, @Nullable String path,
                                     @NonNull ImageView imageView,
                                     @Nullable Drawable loadingImage,
                                     @Nullable Drawable errorImageView) {
        GlideApp.with(context).load(path).placeholder(loadingImage)
                .error(errorImageView).into(imageView);
    }

    public static void loadImageView(@NonNull Context context, @Nullable String path,
                                     @NonNull ImageView imageView,
                                     @Nullable Drawable loadingImage,
                                     @Nullable Drawable errorImageView, @Nullable RequestOptions options) {
        GlideApp.with(context).load(path).placeholder(loadingImage)
                .error(errorImageView).apply(options).into(imageView);
    }

    /**
     * 设置加载中以及加载失败图片
     *
     * @param context        context
     * @param object         object
     * @param imageView      view
     * @param loadingImage   image
     * @param errorImageView view
     */
    public static void loadImageView(@NonNull Context context, @Nullable Object object,
                                     @NonNull ImageView imageView,
                                     @Nullable Drawable loadingImage,
                                     @Nullable Drawable errorImageView) {
        GlideApp.with(context).load(object).placeholder(loadingImage)
                .error(errorImageView).into(imageView);
    }

    /**
     * 设置加载中以及加载失败图片,带回调
     *
     * @param context  context
     * @param object   object
     * @param listener listener
     */
    public static void loadImageView(@NonNull Context context, @Nullable Object object,
                                     @Nullable final OnLoadImageBitmapListener listener) {
        GlideApp.with(context).asBitmap().load(object).into(new SimpleTarget<Bitmap>() {

            @Override
            public void onLoadStarted(@Nullable Drawable placeholder) {
                if (listener != null) {
                    listener.onLoadStarted();
                }
            }

            @Override
            public void onLoadFailed(@Nullable Drawable errorDrawable) {
                if (listener != null) {
                    listener.onLoadFailed();
                }
            }

            @Override
            public void onResourceReady(@NonNull Bitmap resource,
                                        @Nullable Transition<? super Bitmap> transition) {
                if (listener != null) {
                    listener.onLoadSuccess(resource);
                }
            }
        });
    }


    /**
     * 设置加载中以及加载失败图片,带回调
     *
     * @param context  context
     * @param object   object
     * @param listener listener
     */
    public static void loadImageView(@NonNull Context context, @Nullable Object object,
                                     @Nullable final OnLoadImageFileListener listener) {
        GlideApp.with(context).download(object).diskCacheStrategy(DiskCacheStrategy.DATA)
                .into(new SimpleTarget<File>() {

                    @Override
                    public void onLoadStarted(@Nullable Drawable placeholder) {
                        if (listener != null) {
                            listener.onLoadStarted();
                        }
                    }

                    @Override
                    public void onLoadFailed(@Nullable Drawable errorDrawable) {
                        if (listener != null) {
                            listener.onLoadFailed();
                        }
                    }

                    @Override
                    public void onResourceReady(@NonNull File resource,
                                                @Nullable Transition<? super File> transition) {
                        if (listener != null) {
                            listener.onLoadSuccess(resource);
                        }
                    }

                });
    }

    /**
     * 加载Gif图
     *
     * @param context   context
     * @param object    object
     * @param imageView imageView
     */
    public static void loadImageViewAsGif(@NonNull Context context,
                                          @Nullable Object object,
                                          @NonNull ImageView imageView) {
        GlideApp.with(context).asGif().load(object).into(imageView);
    }
    // 需要扩展,暂时只提供前三个方法 其余的先 private

    //加载指定大小
    private static void loadImageViewSize(Context context, String path, int width,
                                          int height, ImageView imageView) {
        GlideApp.with(context).load(path).override(width, height).into(imageView);
    }

    //填充
    private static void loadImageCrop(Context context, String path, ImageView imageView) {
        GlideApp.with(context).load(path).centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.ALL).into(imageView);
    }

    //设置加载中以及加载失败图片并且指定大小
    private static void loadImageViewLoadingSize(Context context, String path,
                                                 int width, int height,
                                                 ImageView imageView,
                                                 int loadingImage, int errorImageView) {
        GlideApp.with(context).load(path).override(width, height)
                .placeholder(loadingImage).error(errorImageView).into(imageView);
    }

    //设置跳过内存缓存
    private static void loadImageViewCache(Context context, String path, ImageView imageView) {
        GlideApp.with(context).load(path).skipMemoryCache(true).into(imageView);
    }

    //设置下载优先级
    private static void loadImageViewPriority(Context context, String path, ImageView imageView) {
        GlideApp.with(context).load(path).priority(Priority.NORMAL).into(imageView);
    }

    /**
     * 策略解说：
     * all:缓存源资源和转换后的资源
     * none:不作任何磁盘缓存
     * source:缓存源资源
     * result：缓存转换后的资源
     */

    //设置缓存策略
    private static void loadImageViewDiskCache(Context context, String path,
                                               ImageView imageView) {
        GlideApp.with(context).load(path).diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageView);
    }

    /**
     * 会先加载缩略图
     */

    //设置缩略图支持
    public static void loadImageViewThumbnail(Context context, String path, ImageView imageView) {
        GlideApp.with(context).load(path).thumbnail(0.1f).into(imageView);
    }

    /**
     * api提供了比如：centerCrop()、fitCenter()等
     */

    //设置动态转换
    private static void loadImageViewCrop(Context context, String path, ImageView imageView) {
        GlideApp.with(context).load(path).centerCrop().into(imageView);
    }

    //清理磁盘缓存
    private static void guideClearDiskCache(Context context) {
        //理磁盘缓存 需要在子线程中执行
        GlideApp.get(context).clearDiskCache();
    }

    //清理内存缓存
    private static void guideClearMemory(Context context) {
        //清理内存缓存  可以在UI主线程中进行
        GlideApp.get(context).clearMemory();
    }
}
